'use strict';

const Controller = require('egg').Controller;

// import {objectId } from 'mongodb'
const {ObjectId}  = require('mongodb')

class DoFormController extends Controller {
  async index() {
    const { ctx } = this;
    var id = ctx.query.id
    var db = this.app.mongodb
    var arr = ['switch','box','gmk']
    for(var i = 0 ; i < arr.length ; i++){
      var res = await db.collection(arr[i]).find({_id:ObjectId(id)}).toArray()
      if(res.length){
        await db.collection(arr[i]).deleteOne({_id:ObjectId(id)})
        ctx.body = `<script>alert("删除成功！");location.href='/home/${arr[i]}';</script>`
      }
    }
  }
}

module.exports = DoFormController;
